﻿///////////////////////////////////////////////////////////////////////////
// 文件名:     progresswidget.cpp
// 描述:
// 作者:      颜磊
// 创建时间:   2024-06-09 00:50
///////////////////////////////////////////////////////////////////////////
#include "progresswidget.h"
#include "ui_progresswidget.h"

#include <QDebug>
#include <QMenu>
#include <qstringutils.h>

ProgressWidget::ProgressWidget(ProgressInfo &progressInfo, QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::ProgressWidget)
{
    ui->setupUi(this);
    setProgressInfo(progressInfo);
    screenshotPixmap = QPixmap(100, 100);
}

ProgressWidget::~ProgressWidget()
{
    delete ui;
}

void ProgressWidget::setScreenshotData(QByteArray data)
{
    screenData = data;
    QPixmap pixmap;
    pixmap.loadFromData(screenData);
    screenshotPixmap = pixmap.scaled(ui->screenshot->width(), ui->screenshot->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->screenshot->setPixmap(screenshotPixmap);
    signal_ScreenshotData(screenData);
}

void ProgressWidget::setScreenshotPixmap(QPixmap pixmap)
{
    pixmap.loadFromData(screenData);
    screenshotPixmap = pixmap.scaled(ui->screenshot->width(), ui->screenshot->height(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    ui->screenshot->setPixmap(screenshotPixmap);
    signal_ScreenshotData(screenData);
}

void ProgressWidget::on_ProgressWidget_customContextMenuRequested(const QPoint &pos)
{
    //    qDebug() << progressObject.connectionInfo.connected;
    //    QMenu menu(this);
    //    menu.addAction(QString("查看大图"),this,&ProgressWidget::slot_seeProgress);

    //    menu.addAction(QIcon(":/res/img/ico/edit.ico"),QString("修改"),this,&ProgressWidget::slot_upProgress);
    //    if(progressInfo.getEnabled()){
    //        menu.addAction(QIcon(":/res/img/ico/stop.ico"),QString("停止"),this,&ProgressWidget::slot_stopProgress);
    //    }else{
    //        menu.addAction(QIcon(":/res/img/ico/start.ico"),QString("启动"),this,&ProgressWidget::slot_startProgress);
    //    }
    //    menu.addSeparator();
    //    menu.addAction(QIcon(":/res/img/ico/delete.ico"),QString("删除"),this,&ProgressWidget::slot_delProgress);
    //    menu.exec(QCursor::pos());
}

void ProgressWidget::slot_seeProgress()
{
    //    ProgressScreenDialog *dialog = new ProgressScreenDialog(this);
    //    connect(this,&ProgressWidget::signal_ScreenshotData,dialog,&ProgressScreenDialog::slot_ScreenshotData);
    //    signal_ScreenshotData(screenData);
    //    dialog->exec();
}

void ProgressWidget::slot_upProgress()
{
    //    ProgressInfo progressInfo = progressObject.progressInfoPack.info;
    //    signal_editProgress(progressObject.connectionInfo.id,progressInfo);
}

void ProgressWidget::slot_delProgress()
{
    //    ProgressCmdHeader progressCmdHeader;
    //    progressCmdHeader.cmdType = PROGRESS_CMD_DEL;
    //    progressCmdHeader.progressId = progressObject.progressInfoPack.info.id;
    //    signal_progressControl(progressObject.connectionInfo.id,progressCmdHeader);
}

void ProgressWidget::slot_startProgress()
{
    //    ProgressCmdHeader progressCmdHeader;
    //    progressCmdHeader.cmdType = PROGRESS_CMD_START;
    //    progressCmdHeader.progressId = progressObject.progressInfoPack.info.id;
    //    signal_progressControl(progressObject.connectionInfo.id,progressCmdHeader);
}

void ProgressWidget::slot_stopProgress()
{
    //    ProgressCmdHeader progressCmdHeader;
    //    progressCmdHeader.cmdType = PROGRESS_CMD_STOP;
    //    progressCmdHeader.progressId = progressObject.progressInfoPack.info.id;
    //    signal_progressControl(progressObject.connectionInfo.id,progressCmdHeader);
}

ProgressInfo ProgressWidget::getProgressInfo() const
{
    return progressInfo;
}

void ProgressWidget::setProgressInfo(const ProgressInfo &value)
{
    progressInfo = value;
    //    qDebug() << __FUNCTION__ << QString("进程窗口：更新信息！") << progressObject.connectionInfo.connected;
    ui->name->setText(progressInfo.getProgress().getName());
    ui->cpuUse->setValue(progressInfo.getCpuUsage());
    ui->cpuUse->setFormat(QString("(进程)CPU:%1%").arg(QString::number(progressInfo.getCpuUsage(), 'f', 2)));
    ui->memory->setText(QStringUtils::numberDivision(progressInfo.getMemory(), 3, 0) + " B");
    ui->ioRead->setText(QStringUtils::numberDivision(progressInfo.getIoRead(), 3, 0) + " B");
    ui->IoWrite->setText(QStringUtils::numberDivision(progressInfo.getIoWrite(), 3, 0) + " B");
    ui->state->setText(progressInfo.getEnabled() ? QString("运行") : QString("停止"));
    ui->stateMsg->setText(progressInfo.getEnabled() ? QStringUtils::formatBySecond(progressInfo.getRunTime()) : progressInfo.getStateMsg());
    ui->state->setStyleSheet(progressInfo.getEnabled() ? "color:green;" : "color:red");
    ui->stateMsg->setStyleSheet(progressInfo.getEnabled() ? "color:green;" : "color:red");
}
